package com.dabai.dbutils.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesBasicUse {

    public static String prefs_name = "config";

    private static SharedPreferences settings;

    /**
     * 初始化
     *
     * @param context   上下文
     * @param prefsName 首选项名称
     */
    public static void init(Context context,String prefsName) {
        prefs_name = prefsName;
        settings = context.getSharedPreferences(prefs_name, Context.MODE_PRIVATE);
    }

    /**
     * 初始化
     *
     * @param context 上下文
     */
    public static void init(Context context) {
        settings = context.getSharedPreferences(prefs_name, Context.MODE_PRIVATE);
    }

    /**
     * 获取布尔
     *
     * @param key      关键
     * @param defValue def价值
     * @return boolean
     */
    public static boolean getBoolean(String key, boolean defValue) {
        return settings.getBoolean(key, defValue);
    }

    /**
     * 设置布尔
     *
     * @param key   关键
     * @param value 价值
     * @return boolean
     */
    public static boolean setBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }


    /**
     * 得到整数
     *
     * @param key      关键
     * @param defValue def价值
     * @return int
     */
    public static int getInt(String key, int defValue) {
        return settings.getInt(key, defValue);
    }

    /**
     * 设置int
     *
     * @param key   关键
     * @param value 价值
     * @return boolean
     */
    public static boolean setInt(String key, int value) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }


    /**
     * 得到字符串
     *
     * @param key      关键
     * @param defValue def价值
     * @return {@link String}
     */
    public static String getString(String key, String defValue) {
        return settings.getString(key, defValue);
    }

    /**
     * 设置字符串
     *
     * @param key   关键
     * @param value 价值
     * @return boolean
     */
    public static boolean setString(String key, String value) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }


    /**
     * 获取long值
     *
     * @param key      关键
     * @param defValue def价值
     * @return long
     */
    public static long getLong(String key, long defValue) {
        return settings.getLong(key, defValue);
    }

    /**
     * 设置long
     *
     * @param key   关键
     * @param value 价值
     * @return boolean
     */
    public static boolean setLong(String key, long value) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    /**
     * 得到浮动
     *
     * @param key      关键
     * @param defValue def价值
     * @return long
     */
    public static float getFloat(String key, float defValue) {
        return settings.getFloat(key, defValue);
    }

    /**
     * 设置浮动
     *
     * @param key   关键
     * @param value 价值
     * @return boolean
     */
    public static boolean setFloat(String key, float value) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }



}